package top.kikt.book.core.handlers

import okhttp3.HttpUrl.Companion.toHttpUrl
import org.jsoup.nodes.Document
import top.kikt.book.core.entity.BookMetaEntity
import top.kikt.book.core.entity.ChapterEntity
import top.kikt.book.core.entity.ChapterMetaEntity
import top.kikt.book.core.handlers.common.BaseBookHandler
import top.kikt.book.util.bookText
import top.kikt.book.util.metaContentWithProperty
import top.kikt.book.util.toChapterMetaList
import java.nio.charset.Charset

@Suppress("ClassName")
class `7yyq` : BaseBookHandler() {
    override val host: String
        get() = "www.7yyq.com"

    override fun parseBookMeta(document: Document, url: String): BookMetaEntity {
        val name = document.metaContentWithProperty("og:novel:book_name")
        val author = document.metaContentWithProperty("og:novel:author")
        val cover = document.metaContentWithProperty("og:image")
        return BookMetaEntity(name, url, url, author, cover)
    }

    override fun forceCharset(): Charset {
        return Charset.forName("gbk")
    }

    override fun parseChapterMeta(document: Document, menuUrl: String) =
        document.toChapterMetaList("#list > dl > dd > a", menuUrl)

    override fun parseChapter(chapterMetaEntity: ChapterMetaEntity): ChapterEntity? {
        val doc = chapterMetaEntity.url.toHttpUrl().toDocument()
        val bookText = doc?.getElementById("content")?.bookText() ?: return null
        return ChapterEntity(chapterMetaEntity.name, chapterMetaEntity.index, bookText)
    }
}